package client;

import java.io.IOException;

import javax.swing.JOptionPane;

import control.Login;

import msg.Message;
import ocsf.client.AbstractClient;

/**
 * Class manages the access to the customer and hold a connection to the server
 * @author Group 19
 * 
 */
public class Client extends AbstractClient{
	
	public Login login;
	
	
/**
 * constructor that open connection to the server
 * @param host - the IP to the host server
 * @param port - the port that the server listens to him
 * @throws IOException
 * 
 */
	public Client(String host, int port) throws IOException {
		super(host, port);
		
		//add controllers here:
		this.login = new Login(this);
		

		
		//try open connection to server
		try {
			openConnection();
			System.out.println("open connection succseded at " +
					"host=" + host + "  " + "port=" + port);
		} catch (IOException e) {
			e.printStackTrace();
			System.out.println("failed to open connection at " +
					"host=" + host + "  " + "port=" + port);
			throw e;
		}
	}
	
	/**
	 *  This method get the result from the server, and execute the suitable method that return to customer.
	 */
	@Override
	protected void handleMessageFromServer(Object msg) {
		Message message = (Message)msg;	
		message.executeClient(this);
	}
	/**
	 * The method show message dialog when the connection to the server is closed
	 */
	protected void connectionClosed()
	{
		JOptionPane.showMessageDialog(null, "You have been disconnected from server",
				"Connection closed", JOptionPane.INFORMATION_MESSAGE);
		System.exit(0);
	}
	
	
}
